/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.dao.impl;

import com.jspx.txweb.table.MemberTree;
import com.jspx.txweb.dao.MemberTreeDAO;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.StringUtil;

import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2005-4-13
 * Time: 10:46:40
 */
public class MemberTreeDAOImpl extends JdbcOperations implements MemberTreeDAO
{
    private List<MemberTree>  cache = null;
    public MemberTreeDAOImpl()
    {

    }

    private String namespace = StringUtil.empty;

    public void setNamespace(String namespace)
    {
        this.namespace = namespace;
    }

    public String getNamespace()
    {
        return namespace;
    }

    /**
     * @param uid 用户名称
     * @return list<ManTree> 得到用户树
     */
    @SuppressWarnings("unchecked")
    public List<MemberTree> getMemberTree(long uid)
    {
        if (cache == null || cache.isEmpty())
        {
            cache = (List<MemberTree>) createCriteria(MemberTree.class)
                    .add(Expression.eq("namespace", namespace)).list(false);
        }
        List<MemberTree> result = new ArrayList<MemberTree>();
        for (MemberTree memberTree :cache)
        {
           if (memberTree.getUid()==uid)
           {
               result.add(memberTree);
           }
        }
        return result;

    }

    /**
     * @param uid 用户名称
     * @return 得到用户树map
     */
    public Map<String, MemberTree> getMemberTreeMap(long uid)
    {
        Map<String, MemberTree> map = new LinkedHashMap<String, MemberTree>();
        List<MemberTree> list = getMemberTree(uid);
        for (Object o : list)
        {
            MemberTree manTree = (MemberTree) o;
            map.put(manTree.getNodeId(), manTree);
        }
        return map;
    }

    /**
     *
     * @param uid  用户id
     * @return String[]   返回用户树,格式为 jcms1;jcms2;jcms3
     */
    public String getMemberTreeSplitString(long uid)
    {
        List<MemberTree> list = getMemberTree(uid);
        if (list == null) return StringUtil.empty;
        StringBuilder sb = new StringBuilder();
        for (MemberTree memberTree :list)
        {
            sb.append(memberTree.getNodeId()).append(StringUtil.SEMICOLON);
        }
        return sb.toString();
    }

    /**
     *
     * @param uid  用户id
     * @return  用户树
     */
    public String[] getMemberTreeArray(long uid)
    {
        String[] result = new String[0];
        List<MemberTree> list = getMemberTree(uid);
        if (list == null) return new String[0];
        for (MemberTree memberTree :list)
        {
            result = ArrayUtil.add(result, memberTree.getNodeId());
        }
        return result;
    }
    /**
     * @return boolean
     */
    public boolean deleteAll() throws Exception
    {
        return createCriteria(MemberTree.class)
                .add(Expression.eq("namespace", namespace))
                .delete(false)>0;
    }

    /**
     * @param uid  用户id
     * @return boolean
     */
    public boolean deleteForUid(long uid) throws Exception
    {
        return createCriteria(MemberTree.class)
                .add(Expression.eq("namespace", namespace))
                .add(Expression.eq("uid", uid))
                .delete(false)>0;
    }

    /**
     *
     * @param treeItemId 用户id
     * @return boolean   删除不是本数的节点
     */
    public boolean fixTreeItem(String[] treeItemId) throws Exception
    {
        return createCriteria(MemberTree.class)
                .add(Expression.eq("namespace", namespace))
                .add(Expression.not(Expression.in("nodeId", treeItemId)))
                .delete(false)>0;
    }

    /**
     * 刷新
     */
    public void flush()
    {
        if (cache != null)
            cache.clear();
    }

}